function emmm_login(username,password) {
  document.getElementById("username").value=username;
  document.getElementById("password").value=password;
  document.getElementById("login-btn").click();
}
function emmm_getNearBooks() {
  var books = getBook();
  var result=[];
  if (books.length)
  {
    for (var i = 0; i < books.length; i++)
    {
      result[i]={};
      result[i].chapter_code=books[i][1];
      result[i].book_code=books[i][0];
      result[i].chapter_name="最近阅读:".concat(books[i][3]);
      result[i].book_title=books[i][2];
      result[i].loading=true;
      result[i].id=i;
    }
  }
  else
  {
    result[0]="无";
  }
  console.log(JSON.stringify(result));
}
function emmm_getSorts() {
  var nodes=document.getElementsByClassName("sort c_sort")[0].childNodes[1].childNodes;
  var result=[];
  for(j = 0 , i=0 , len=nodes.length; j < len; j++) {
    if (nodes[j].childNodes.length!=0) {
      result[i]={};
      result[i].sort_name=nodes[j].childNodes[0].innerHTML;
      result[i].sort_code=nodes[j].childNodes[0].href.replace(/https:\/\/.+sort=/g,"").replace(/&.+/g,"");
      result[i].id=i;
      i++;
    }
  }
  console.log(JSON.stringify(result));
}
function emmm_getTags() {
  var nodes=document.getElementById("tag_ul").childNodes;
  var result=[];
  for(j = 0 , i=0 , len=nodes.length; j < len; j++) {
    if (nodes[j].childNodes.length!=0) {
      result[i]={};
      result[i].tag_name=nodes[j].childNodes[0].innerHTML;
      result[i].tag_code=nodes[j].childNodes[0].href.replace(/https:\/\/.+\/tag\//g,"").replace(/\//g,"");
      result[i].id=i;
      i++;
    }
  }
  console.log(JSON.stringify(result));
}
function emmm_getCharts() {
  var nodes=document.getElementById("tab").childNodes;
  var result=[];
  for(j = 0 , i=0 , len=nodes.length; j < len; j++) {
    if (nodes[j].childNodes.length!=0) {
      result[i]={};
      result[i].chart_name=nodes[j].innerHTML;
      result[i].chart_code=nodes[j].attributes.attr.nodeValue;
      result[i].id=i;
      i++;
    }
  }
  console.log(JSON.stringify(result));
}
function emmm_getLastBooks() {
  var nodes=document.getElementById("last_ul").childNodes;
  var result=[];
  for(j = 0 , i=0 , len=nodes.length; j < len; j++) {
    if (nodes[j].childNodes.length!=0) {
      result[i]={};
      result[i].book_code=nodes[j].childNodes[0].href.replace(/https:\/\/.+\/novel\//g,"").replace(/.html/g,"");
      result[i].book_title=nodes[j].childNodes[0].innerHTML.replace(/-.*/g,"");
      result[i].sort_name=document.getElementById("last_ul").childNodes[0].childNodes[1].childNodes[2].innerHTML;
      result[i].loading=true;
      result[i].synopsis="";
      result[i].id=i;
      i++;
    }
  }
  console.log(JSON.stringify(result));
}
function emmm_getBookInfo() {
  var nodes=document.getElementsByClassName("article_info_td")[0].childNodes;
  var result={};
  result.writer=document.getElementsByClassName("article_info_td")[0].childNodes[1].childNodes[1].innerText;
  result.time=document.getElementsByClassName("article_info_td")[0].childNodes[3].childNodes[1].innerText;
  var dislike=parseInt(document.getElementById("sgrade0").childNodes[3].innerText);
  var normal=parseInt(document.getElementById("sgrade1").childNodes[3].innerText);
  var like=parseInt(document.getElementById("sgrade2").childNodes[3].innerText);
  result.starPeople=dislike+normal+like;
  result.star=Math.round((dislike*0+normal*5+like*10)/result.starPeople*100)/100;
  result.synopsis=document.getElementsByTagName("pre")[0].innerText.replace(/\n\n/g,"\n");
  if (document.getElementById("login_top").childNodes[0].nodeName!="DIV") {
    result.myStar="未登录";
  } else if (document.getElementsByClassName("sg-bg").length==0) {
    result.myStar="未评分";
  } else {
    var starName=["不好看(0分)","还可以(5分)","很不错(10分)"]
    result.myStar=starName[parseInt(document.getElementsByClassName("sg-bg")[0].id.replace(/sgrade/g,""))];
  }
  console.log(JSON.stringify(result));
}
function emmm_isLogin() {
  console.log(document.getElementById("login_top").childNodes[0].nodeName=="DIV");
}
function emmm_ajaxLogin() {
  function emmm_getPrev(body){
    if (body.match(/<form([\s\S]*)<\/form>/gi)) {
      var prev=body.match(/<form([\s\S]*)<\/form>/gi)[0].match(/<input type="hidden"([^>]*)>/gi);
      var prev_r="";
      for (var index in prev) {
        if (prev.hasOwnProperty(index)) {
          var name=prev[index].match(/name="([^"]*)"/gi)[0].replace("name=\"","").replace("\"","");
          var value=prev[index].match(/value="([^"]*)"/gi)[0].replace("value=\"","").replace("\"","");
          prev_r=prev_r+"&"+name+"="+value;
        }
      }
      doAjax("member/login/","user=3440454510&pass=zhang460106&imgcode=KFZF"+prev_r,"emmm_login", "POST", 0);
    } else {
      emmm_login(body);
    }
  };
  function emmm_login(body){
      callApp("登录","",JSON.stringify([body.replace(/\n/gi,"").replace(/<!DOCTYPE.+<\/h1>/gi,"").replace(/<\/p>.+<\/html>/gi,"").replace(/<p class=".+">/gi,"")]));
  };
  doAjax("member/log/","","emmm_getPrev", "GET", 0);
}
function 获取书架() {
  function emmm_getBookshelf(body){
    var 内容=body.replace(/\n/gi,"").replace(/\t/gi,"").replace(/<!DOCTYPE.+<\/style>/gi,"").replace(/<div class="lb_top c_big lb_topshow">.+<div style="margin-top:45px;">/gi,"").replace(/<\/div><div id="foot" class="foot">.+<\/html>/gi,"");
    if (内容=="") {
      var result=["无"];
    } else {
      var result=[];
      var 停止=false;
      var i=0;
      while (停止!=true) {
        var 书str=内容.replace(/<\/table><table class='bookone'.+<\/table>/gi,"</table>")
        .replace(/<\/table><table class='bookone'.+<\/table>/gi,"</table>")
        .replace(/<\/table><table class='bookone'.+<\/table>/gi,"</table>")
        .replace(/<\/table><table class='bookone'.+<\/table>/gi,"</table>")
        .replace(/<\/table><table class='bookone'.+<\/table>/gi,"</table>")
        .replace(/<\/table><table class='bookone'.+<\/table>/gi,"</table>");
        if (书str==内容) {
          停止=true;
        }
        内容=内容.replace(书str,"");
        result[i]={};
        var 书签=书str.replace(/<table class='bookone'.+class='case_shuqian'>/gi,"").replace(/<\/td><\/tr><tr><td class='case_del'>.+<\/font><\/a><\/td><\/tr><\/table><\/td><\/tr><\/table>/gi,"");
        if (书签=="书签：无书签") {
          result[i].chapter_code="无";
          result[i].chapter_name="还未开始阅读";
        } else {
          result[i].chapter_code=书签.replace(/.html">.+<\/a>/gi,"").replace(/书签：<a href="\/novel\/.+\//gi,"");
          result[i].chapter_name="最近阅读:".concat(书签.replace(/书签：.+.html">/gi,"").replace("</a>",""));
        }
        result[i].book_code=书str.replace("<table class='bookone' id=","").replace(/><tr><td><table><tr><td class='case_name'>书名：.+<\/font><\/a><\/td><\/tr><\/table><\/td><\/tr><\/table>/gi,"");
        result[i].book_title=书str.replace(/<table class='bookone' id=.+<td class='case_name'>书名：/gi,"").replace(/<\/a><\/td><\/tr><tr><td class='case_shuqian.+<\/font><\/a><\/td><\/tr><\/table><\/td><\/tr><\/table>/gi,"").replace(/<a href='.+'>/gi,"");
        result[i].loading=true;
        result[i].id=i;
        i++;
      }
    }
    console.log(JSON.stringify(result));
  };
  doAjax("bookcase/","","emmm_getBookshelf", "GET", 0);
}
function 获取图片验证码() {
  function getBase64Image(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0, img.width, img.height);
    var dataURL = canvas.toDataURL("image/png");
    return dataURL;
  }
  var img = document.createElement('img');
  img.onload =function() {
    var data = getBase64Image(img);
    var result={};
    result.back="回调方法";
    result.data=data;
    callApp("登录","",JSON.stringify(result));
  }
  img.src = '/tool/getVerify/?d='+Math.random();
  document.body.appendChild(img);
}
function 获取书籍详情() {
  var book_code="90298";
  function emmm_getBook(body){
    var result={}
    result.book_code="90298";
    result.writer=body.match(/by (.+)<\/title>/gi)[0].replace("by ","").replace("<\/title>","");
    result.time=body.match(/<div>更新时间：<time>(.+)<\/time><\/div>/gi)[0].replace("<div>更新时间：<time>","").replace("<\/time><\/div>","");
    result.synopsis=body.match(/name="description"([\s\S]*)name="MobileOptimized"/gi)[0].replace("name=\"description\" content=\"","").replace(/" \/>([\s\S]*)MobileOptimized"/gi,"");
    var token=body.match(/\/tool\/displayGrade\/([^,]*)\//gi)[0]
    var xhr;
    if(window.XMLHttpRequest){
      xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
      xhr = new window.ActiveXObject();
    }else{
      alert("请升级至最新版本的浏览器");
    }
    if(xhr !=null){
      xhr.open("GET",token,true);
      xhr.send(null);
      xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
          var res = JSON.parse(xhr.responseText);
          result.starPeople=Number(res[0].total)+Number(res[1].total)+Number(res[2].total);
          result.star=Math.round((Number(res[0].total)*0+Number(res[1].total)*5+Number(res[2].total)*10)*100/result.starPeople)/100;
          if (res[3].isGrade==0) {
            result.myStar="未评分";
            result.myStarState=false;
          } else {
            var starName=["不好看(0分)","还可以(5分)","很不错(10分)"];
            result.myStar=starName[res[3].vote];
            result.myStarState=true;
          }
          //console.log(res);
          console.log(JSON.stringify(result));
        }
      };
    }
    //console.log(body.replace(/\n/gi,"").replace(/\t/gi,""));
    //console.log(result);
  };
  doAjax("novel/"+book_code+".html","","emmm_getBook", "GET", 0);
}
function 获取章节内容() {
  function emmm_getChapter(body){
    var result={};
    result.book_code=90678;
    result.book_title=secret(body.match(/#nr_title([^;]*)\;/gi)[0].match(/\"(.+)\"/gi)[0].replace(/\"/gi,""),body.match(/#nr_title([^;]*)\;/gi)[0].match(/\, \'(.+)\'\,/gi)[0].replace(/[', ]/gi,""),true);
    result.chapter_name=body.match(/#nr_title([^;]*)\;/gi)[0].match(/\+ \'(.+)\'/gi)[0].replace(/\'/gi,"").replace("+  - ","");
    result.hasTitle=true;
    shuqian(90696,12050136,"分卷阅读67")
    var aj = $.ajax( {
      url:'/api/chapter/',
      data:{
        aid : 90678,
        cid : 12047506,
      },
      type:'post',
      cache:true,
      dataType:'json',
      success:function(data) {
        if(data.status == 1 ){
          if (data.page.next=="") {
            result.nextChapter_code="无";
          } else {
            result.nextChapter_code=data.page.next.replace("/","");
          }
          if (data.page.prev=="") {
            result.lastChapter_code="无";
          } else {
            result.lastChapter_code=data.page.prev.replace("/","");
          }
          result.chapter_inner=secret(data.data, data.key, true).match(/<p>(.+)<\/p>/gi);
          for (var i in result.chapter_inner){
            if (result.chapter_inner.hasOwnProperty(i)) {
              result.chapter_inner[i]=result.chapter_inner[i].replace("<p>","").replace("</p>","");
            }
          }
          console.log(JSON.stringify(result));
        }else{
          alert("章节不存在！");
        }
       },
       error : function() {
        alert("异常！");
       }
    });
  };
  doAjax("novel/90678/12047506.html","","emmm_getChapter", "GET", 0);
}
